﻿using System.Collections.Generic;
using ComputationalGeometry.DataStructures;
using NUnit.Framework;

namespace ComputationalGeometry.UnitTests
{
    [TestFixture]
    public class BinaryTreeTests
    {
        [Test]
        public void AddTest()
        {
            BinaryTree<int> tree = new BinaryTree<int> {8, 3, 11, 1, 5, 10, 12};

            int[] inOrderList = { 1, 3, 5, 8, 10, 11, 12 };
            int[] preOrderList = { 8, 3, 1, 5, 11, 10, 12 };
            int[] postOrderList = { 1, 5, 3, 10, 12, 11, 8 };

            Assert.AreEqual(tree.Count, 7);

            IEnumerable<int> inOrder = tree.Inorder;            
            Assert.IsTrue(inOrderList.IsEqualTo<int>(inOrder));            

            IEnumerable<int> preOrder = tree.Preorder;            
            Assert.IsTrue(preOrderList.IsEqualTo<int>(preOrder));

            IEnumerable<int> postOrder = tree.Postorder;
            Assert.IsTrue(postOrderList.IsEqualTo<int>(postOrder));
        }

        [Test]
        public void RemoveTest()
        {
            BinaryTree<int> tree = new BinaryTree<int> {8, 3, 11, 1, 5, 10, 12};

            Assert.AreEqual(tree.Count, 7);

            tree.Remove(3);
            Assert.AreEqual(tree.Count, 6);
            Assert.IsFalse(tree.Remove(3));
            Assert.IsFalse(tree.Contains(3));

            tree.Remove(10);
            Assert.AreEqual(tree.Count, 5);
            Assert.IsFalse(tree.Remove(10));
            Assert.IsFalse(tree.Contains(10));

            int[] inOrderList = { 1, 5, 8, 11, 12 };
            Assert.IsTrue(inOrderList.IsEqualTo(tree.Inorder));

            tree.Remove(8);
            tree.Remove(11);
            tree.Remove(1);
            tree.Remove(5);
            tree.Remove(12);

            Assert.AreEqual(tree.Count, 0);
            int[] inOrderList2 = { };
            Assert.IsTrue(inOrderList2.IsEqualTo(tree.Inorder));
        }

        [Test]
        public void HeightTest()
        {
            BinaryTree<int> tree = new BinaryTree<int> {8, 3, 11, 1, 2};

            Assert.AreEqual(tree.Height(), 4);

            tree.Remove(2);
            Assert.AreEqual(tree.Height(), 3);

            tree.Remove(1);
            Assert.AreEqual(tree.Height(), 2);

            tree.Remove(3);
            tree.Remove(11);
            Assert.AreEqual(tree.Height(), 1);

            tree.Remove(8);
            Assert.AreEqual(tree.Height(), 0);
        }
    }
}
